Practical Lab 2¶

Sudhan Shrestha - 8889436¶

Importing necesary packages¶

For the graphs we are creating in this notebook the following packages were imported:

  • Matplotlib
  • Plotly
  • Seaborn
  • pandas
  • numpy
In [2]:
import matplotlib.pyplot as plt
import plotly
import plotly.express as px
import plotly.graph_objects as go
import seaborn as sns
import numpy as np
plotly.offline.init_notebook_mode()

BAR GRAPH: MATPLOTLY¶

  • This is a bar-graph created using matplotlib.
  • This bar represents the age of a group of friends.
In [3]:
# Matplotlib Bar-Graph
names = ['Suzan', 'Harry', 'Genji', 'Denver', 'Mandy']
ages = [20, 21, 19, 23, 18]
bar_colors = ['tab:red', 'tab:blue', 'tab:green', 'tab:purple', 'tab:orange']

fig, ax = plt.subplots()
ax.bar(names, ages, label=ages, color=bar_colors)

ax.set(yticks=np.arange(0, 25), xlabel='Name', ylabel='Age', title= 'Ages in a friend group')
plt.show()

PLOLTY: BAR-GRAPH¶

  • Here is the same bargraph using plolty, which is a bit interactive than that of Matplotlib.
  • Created using Plotly Express, a high level interface to Plolty, which can operate on various data.

PLOLTY: BAR-GRAPH GUIDE

In [4]:
colour = ['red', 'blue', 'green', 'purple', 'orange']
fig = px.bar(x=names, y=ages, color=colour)
fig.update_layout(title='Ages in a friend group', yaxis_title='Age',
                  xaxis_title='Name')
fig.show()

PIE CHART: PLOTLY¶

  • A pie-chart creating using plotly.
  • Created using go.Pie, where data visualized by sectors of the pie is set in values.
  • Pie-Chart with go.Pie Guide
In [5]:
# Plotly Pie-Chart

labels = ['Grocery', 'Rent', 'Clothing', 'Entertainment', 'Others']
values = [350, 450, 150, 100, 150]

fig = go.Figure(data=[go.Pie(labels=labels, values=values, title='Monthly Expenses')])
fig.show()

SCATTERPLOT - SEABORN¶

  • A scatterplot created using random
  • Used numpy random to generate random arrays.
In [6]:
# Seaborn scatterplot

x = np.random.normal(size=100)
y = np.random.normal(size=100)
colors = np.random.rand(100)
sizes = 100 * np.random.rand(100)

sns.scatterplot(x=x, y=y, hue=colors, size=sizes)
Out[6]:
<Axes: >

TABLE - PLOTLY¶

  • A table generated using plotly
  • Created using go.Table which provides a Table object for viewing data.
  • Table-Plotly Guide
In [7]:
# Plotly Table
fig = go.Figure(data=[go.Table(header=dict(values=['Grocery', 'Rent', 'Clothing', 'Entertainment', 'Others'], 
                                           line_color='slategrey',fill_color='skyblue', align='left'),
                               cells=dict(values=[[350, 325, 330], [450, 440, 445], [150, 200, 155], [100,90, 120], [150, 400, 203]], 
                                          line_color='slategrey',fill_color='lightcyan', align='left'))])
fig.show()
In [ ]:
#  jupyter nbconvert --to html students_submissions\8889436\practical_lab2.ipynb --output-dir ./docs/8889436 --TagRemovePreprocessor.enabled=True --TagRemovePreprocessor.remove_input_tags="['noshow']"